package com.angel.web.health;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.Pager;
import com.angel.health.BuzFranchisee;
import com.angel.provider.health.BuzFranchiseeProvider;


/**
 * @author hanlinxiu
 *
 */

@Controller
@RequestMapping("/franchisee")
public class BuzFranchiseeController {

	@Autowired
	private BuzFranchiseeProvider provider;
	
	
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(BuzFranchisee franchisee, HttpSession session){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		String userName = (String)session.getAttribute("userName");
		
		franchisee.setCreateBy(userName);
		
		provider.insert(franchisee);
		
		map.put("status", "success");
		map.put("msg", "保存成功");
		
		return map;
	}
	
	/**
	 * 加盟商列表
	 * 
	 * @param buzFranchisee
	 * @param request
	 * @param pager
	 * @return
	 */
	@RequestMapping("/franchisee")
	public String findAll(BuzFranchisee buzFranchisee, HttpServletRequest request, Pager pager){
		
		pager = provider.findAll(buzFranchisee, pager);
		
		request.setAttribute("data", pager);
		
		return "franchisee/franchisee";
		
	}
	
	@RequestMapping("/toFranchiseeAdd")
	public String toFranchiseeAdd(Integer id, String from, ModelMap modelMap){
		
		BuzFranchisee buzFranchisee = null;
		
		if(id != null){
			buzFranchisee = provider.findById(id);
		}
			
		modelMap.put("from", from);
		modelMap.put("data", buzFranchisee);
		
		return "franchisee/franchisee_add";
	}
	
	
	/**
	 * 新增，更新加盟商
	 * 
	 * @param buzInstitutions
	 * @return  
	 */
	@RequestMapping("/addFranchisee")
	@ResponseBody
	public Map<String, Object> addFranchisee(@ModelAttribute("user") String user, @ModelAttribute("buzFranchisee")BuzFranchisee buzFranchisee){
		
		Map<String, Object> map = new HashMap<>();
		
		
		if(buzFranchisee.getName()==null || buzFranchisee.getName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "加盟商名称不能为空!");
			return map;
		}
		
		buzFranchisee.setCreateBy(user);
		
		//如果id不为空，则是审核否则新增
		if(buzFranchisee.getId() == null){
			//未审核
			buzFranchisee.setStatus(0);
			
			buzFranchisee.setCreateBy(user);
			provider.insert(buzFranchisee);
		}else{
			buzFranchisee.setUpdateBy(user);
			provider.updateById(buzFranchisee);
		}
			
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	/**
	 * 删除
	 * 
	 * @param buzFranchisee
	 * @return
	 */
	@RequestMapping("/deleteFranchisee")
	@ResponseBody
	public Map<String, Object> deleteFranchisee(@ModelAttribute("buzFranchisee")BuzFranchisee buzFranchisee){
		
		Map<String, Object> map = new HashMap<>();
		
		provider.delById(buzFranchisee);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	/**
	 * 批量删除
	 *  
	 * @param checks
	 * @return
	 */
	@RequestMapping("/deleteFranchiseeBatch")
	@ResponseBody
	public Map<String,Object> deleteHealthExceptionBatch(@RequestParam("checks[]")List<Integer> checks){
		
		Map<String, Object> map = new HashMap<>();
		
		provider.deleteByKeysSelective(checks);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}

	
	
}
